package com.hc.javabasestudyproject.feature.functionalInterface;

import com.hc.javabasestudyproject.feature.entity.Author;

import java.util.function.Supplier;

/**
 * @Author: huangcheng
 * @CreateTime: 2023-11-22  12:17
 * @Description: 可以用户返回不同类型的结果
 * @Version: 1.0
 */
public class SupplierInterface {

    //需要传入一个函数接口作为参数
    private static String getName(Supplier<Author> supplier){
        return supplier.get().getName();
    }
    //对象的引用调用是一个函数接口，需要函数接口来接受
    private static Supplier getName(){
        Author author = new Author(1L, "蒙多", 33, "一个从菜刀中明悟哲理的祖安人", null);
        return author::getName;
    }
    public static void main(String[] args) {
        //创建函数接口
        Supplier<Author> studentSupplier = new Supplier<Author>() {
            @Override
            public Author get() {
                return new Author(1L, "蒙多", 33, "一个从菜刀中明悟哲理的祖安人", null);
            }
        };
        Supplier<Author> supplier = () -> new Author(1L, "蒙多", 33, "一个从菜刀中明悟哲理的祖安人", null);
        System.out.println(getName(supplier));
        System.out.println(getName().get());
        System.out.println(getName(Author::new));
        System.out.println(getName(()->new Author()));
        // 测试泛型接口
        Double str = getStr(()->2.3);
    }

    public static <R> R getStr(Supplier<R> supplier){
        return supplier.get();
    }
}
